package models

import (
	"time"

	"gorm.io/gorm"
)

// PaymentStatus 支付状态枚举
type PaymentStatus string

const (
	PaymentStatusPending   PaymentStatus = "pending"   // 待支付
	PaymentStatusPaid      PaymentStatus = "paid"      // 已支付
	PaymentStatusFailed    PaymentStatus = "failed"    // 支付失败
	PaymentStatusRefunded  PaymentStatus = "refunded"  // 已退款
	PaymentStatusCancelled PaymentStatus = "cancelled" // 已取消
)

// Payment 支付记录模型
type Payment struct {
	ID            uint           `json:"id" gorm:"primaryKey"`
	GroupID       string         `json:"group_id" gorm:"not null;index"`             // 群号
	ApplicantID   string         `json:"applicant_id" gorm:"not null;index"`         // 申请人ID
	Amount        float64        `json:"amount" gorm:"not null"`                     // 支付金额
	Status        PaymentStatus  `json:"status" gorm:"not null;default:'pending'"`   // 支付状态
	PaymentMethod string         `json:"payment_method" gorm:"size:50"`              // 支付方式
	TransactionID string         `json:"transaction_id" gorm:"size:100;uniqueIndex"` // 交易ID
	PaymentTime   *time.Time     `json:"payment_time"`                               // 支付时间
	Description   string         `json:"description" gorm:"size:500"`                // 描述
	CreatedAt     time.Time      `json:"created_at"`
	UpdatedAt     time.Time      `json:"updated_at"`
	DeletedAt     gorm.DeletedAt `json:"-" gorm:"index"`
}

// GroupApplication 群申请模型
type GroupApplication struct {
	ID          uint           `json:"id" gorm:"primaryKey"`
	GroupID     string         `json:"group_id" gorm:"not null;index"`     // 群号
	ApplicantID string         `json:"applicant_id" gorm:"not null;index"` // 申请人ID
	PaymentID   *uint          `json:"payment_id" gorm:"index"`            // 关联的支付记录ID
	Payment     *Payment       `json:"payment,omitempty" gorm:"foreignKey:PaymentID"`
	Status      string         `json:"status" gorm:"not null;default:'pending'"` // 申请状态
	AppliedAt   time.Time      `json:"applied_at" gorm:"not null"`               // 申请时间
	CreatedAt   time.Time      `json:"created_at"`
	UpdatedAt   time.Time      `json:"updated_at"`
	DeletedAt   gorm.DeletedAt `json:"-" gorm:"index"`
}

// TableName 指定表名
func (Payment) TableName() string {
	return "payments"
}

func (GroupApplication) TableName() string {
	return "group_applications"
}
